package prolog.terms

final case class Cons(h: Term, b: Term)
  extends Fun(".", Array(h, b)) {

  def getHead = args(0).ref
  def getBody = args(1).ref

  override def toString = {
    val s = new StringBuffer()
    s.append("[")
    var x: Term = this
    var more = true
    while (more) {
      val c: Cons = x.asInstanceOf[Cons]
      s.append(c.getHead)
      x = c.getBody
      if (x.isInstanceOf[Cons])
        s.append(",")
      else {
        more = false
        if (x != Const.nil) {
          s.append("|")
          s.append(x)
        }
      }
    }
    s.append("]")
    s.toString
  }
}
